package com.serkol.ams.module.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.module.sys.entity.SysUsers;
import com.serkol.ams.module.sys.service.SysUsersService;
import com.serkol.ams.module.sys.mapper.SysUsersMapper;
import com.serkol.ams.utils.JwtUtils;
import com.serkol.ams.utils.R;
import com.serkol.ams.utils.RedisUtil;
import com.serkol.ams.vo.UserLoginVo;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
* @author Hesen
* @description 针对表【sys_users】的数据库操作Service实现
* @createDate 2024-05-27 19:04:34
*/
@Service
public class SysUsersServiceImpl extends ServiceImpl<SysUsersMapper, SysUsers>
    implements SysUsersService{

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private JwtUtils jwtUtils;
    @Override
    public SysUsers findUserByUserName(String username) {
        if (!StrUtil.isEmpty(username)) {
            LambdaQueryWrapper<SysUsers> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysUsers::getUsername, username);
            SysUsers userEntity = this.getOne(wrapper);
            if (userEntity != null) {
                redisUtil.set("user" + userEntity.getId(), userEntity,600);
            }
            return userEntity;
        }
        return null;
    }

    @Override
    public R getAppLogin(SysUsers user) {
        System.out.println("user:"+user);
            if(!StrUtil.isEmpty(user.getPhone()) && !StrUtil.isEmpty(user.getPassword())){
            /**
             * 查询数据库是否存在该手机号的用户
             */
            LambdaQueryWrapper<SysUsers> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysUsers::getPhone,user.getPhone());
            wrapper.eq(SysUsers::getIsAdmin,0);
            SysUsers one = this.getOne(wrapper);

            if(one != null){
                  if(one.getDeleted() == 1){
                return R.error("账号已被禁用!");
            }
                LambdaQueryWrapper<SysUsers> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SysUsers::getPhone,user.getPhone());
                BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
                String encode = bCryptPasswordEncoder.encode(user.getPassword());
                System.out.println("encode:"+encode);
                boolean matches = bCryptPasswordEncoder.matches(user.getPassword(), one.getPassword());
                if(matches){
                    UserLoginVo userLoginVo = new UserLoginVo();
                    BeanUtil.copyProperties(one, userLoginVo);
                    //生成token
                    String token = JwtUtils.getJwtToken(one.getId()+"",one.getUsername());
                    userLoginVo.setToken(token);
                    //将token信息保存到Redis中
                    String tokenKey = "token_"+token;
                    redisUtil.set(tokenKey, token,18000);
                    /**
                     * 数据放入到redis中  30分钟过期
                     */
                    redisUtil.set(one.getUsername()+"_"+one.getId(),userLoginVo,18000);
                    return R.ok("登录成功!").put("data", userLoginVo);
                }
                return R.error("手机号或密码错误!");
            }else {
                 return R.error("手机号不存在!请先注册");
            }

        }
        return R.error("手机号或密码不能为空!");
    }
}




